// pages/bookEnter/record/erRecord/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [{
        title: "类型",
        content: ["热门", "新书", "好评", "完结"],
        eng: ["hot", "new", "reputation", "over"],
        styleBgcolor: ["#f0f0f0", "", "", ""]
      },
      {
        title: "分类",
        content: [],
        styleBgcolor: []
      }
    ],

    id: "",
    index: "",
    bookData: "", // 获取到的数据，以数组形式存在
    total: 0, //获取到的书本数量
    flog: 0, // 用来标记页码。
    pageHidden: false, // 用来给页码元素做影藏
    tap: 20, // 表示每次显示的数据的数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = options.id;
    let index = options.index;
    let lists = this.data.lists;

    this.setData({
      id,
      index
    })
    wx.request({
      url: 'https://api.zhuishushenqi.com/cats/lv2',
      success: res => {
        res.data[index].forEach(val => {
          if (val.major === id) {
            if (val.mins.length) {
              lists[1].content = val.mins

              for (let i = 0; i < val.mins.length; i++) {
                if (i === 0) {
                  lists[1].styleBgcolor[i] = "#f0f0f0"
                } else {
                  lists[1].styleBgcolor[i] = ""
                }
              }
              this.setData({
                lists
              })
              this.geterRecordData()
              return;
            } else {
              lists[1].content[0] = id;
              lists[1].styleBgcolor[0] = "#f0f0f0"
              this.setData({
                lists
              })
              this.geterRecordData()
            }
          }
        })
      }
    })
  },


  geterRecordData(num1 = 0) {
    let num2 = this.data.tap;
    this.setData({
      bookData: ""
    })
    let id = this.data.id;
    let index = this.data.index;

    let cs1 = this.getCs(0);
    let cs2 = this.getCs(1);

    cs1 = this.data.lists[0].eng[cs1];
    cs2 = this.data.lists[1].content[cs2];

    wx.request({
      url: `https://api.zhuishushenqi.com/book/by-categories?gender=${index}&type=${cs1}&major=${id}&minor=${cs2}&start=${num1}&limit=${num2}`,
      success: res => {

        res.data.books.forEach(val => {
          let num = val.cover.indexOf("http");
          val.cover = decodeURIComponent(val.cover.slice(num));
        })

        this.setData({
          total: res.data.total
        })
        this.setData({
          bookData: res.data.books
        })

        if (this.data.total > num1 + this.data.tap) {
          this.setData({
            pageHidden: true
          })
        } else {
          this.setData({
            pageHidden: false
          })
        }
      }
    })
  },

  submit(e) { // 子组件传递过来的方法
    this.setData({
      flog: 0
    })
    let number = e.detail.target.id;
    number = number.split("-");
    let lists = this.data.lists;
    lists[number[0]].styleBgcolor = lists[number[0]].styleBgcolor.map((val, index) => {
      if (index === parseInt(number[1])) {
        val = "#f0f0f0"
      } else {
        val = ""
      }
      return val;
    })
    this.setData({
      lists
    })
    this.geterRecordData()
  },

  getCs(num) {
    let lists = this.data.lists;
    let number = 0;
    lists[num].styleBgcolor.forEach((val, index) => {
      if (val !== "") {
        number = index;
        return;
      }
    })
    return number
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  nextPage() { // 翻下一页
    let flog = ++this.data.flog;
    let tap = this.data.tap;
    flog = flog * tap;
    this.geterRecordData(flog)
  },
})